package com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.group;

import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.ConditionLogic;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.PerformanceBizType;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.PerformanceRuleType;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PerformanceRule;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PerformanceRuleCondition;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PerformanceRuleGroup;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.Range;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.BizRuleGroup;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.rule.PowerAfterChangeRule;
import lombok.Data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 换电后可用电量配置
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/19
 */
@Data
public class PowerAfterChangeBasedGroup implements BizRuleGroup {
    /**
     * 是否开启
     */
    private Boolean enabled;
    /**
     * 换电后电量绩效规则
     */
    private List<PowerAfterChangeRule> subRules;

    @Override
    public PerformanceRuleGroup toRaw(PerformanceBizType bizType, Integer accountId) {
        PerformanceRuleGroup group = new PerformanceRuleGroup();
        group.setAccountId(accountId);
        group.setBizType(bizType);
        group.setRuleType(PerformanceRuleType.POWER_AFTER_CHANGE);
        group.setEnabled(enabled);
        group.setRules(new ArrayList<>());

        if (subRules == null) {
            return group;
        }
        for (PowerAfterChangeRule rule : subRules) {
            PerformanceRule performanceRule = new PerformanceRule();
            if (rule.getPerformance() == null) {
                throw new BaseException("换电后可用电量绩效-绩效值不能为空");
            }
            performanceRule.setPerformance(rule.getPerformance());
            performanceRule.setConditionLogic(ConditionLogic.AND);
            performanceRule.setConditions(new ArrayList<>());

            // 换电后可用电量条件
            if (rule.getAfterChangePower() == null) {
                throw new BaseException("换电后可用电量绩效-换电后可用电量区间不能为空");
            }
            performanceRule.getConditions().add(rule.getAfterChangePower().toCondition("afterChangePower"));

            group.getRules().add(performanceRule);
        }
        return group;
    }

    @Override
    public void initFromRaw(PerformanceRuleGroup group) {
        setEnabled(group.getEnabled());
        subRules = new ArrayList<>();
        for (PerformanceRule rule : group.getRules()) {
            PowerAfterChangeRule subRule = new PowerAfterChangeRule();
            for (PerformanceRuleCondition condition : rule.getConditions()) {
                if ("afterChangePower".equals(condition.getKey())) {
                    subRule.setAfterChangePower(Range.from(condition, Integer::parseInt));
                }
            }
            subRule.setPerformance(rule.getPerformance());
            subRules.add(subRule);
        }
    }

    @Override
    public void initDefault() {
        enabled = false;
        subRules = Collections.emptyList();
    }
}
